'use strict';

const Controller = require('egg').Controller;

var Mock = require('mockjs');
let Random = Mock.Random;
var mockPage = require('../../utils/mockPage');
var mockData = require('./data.js');

/**
 * @Controller 模拟通用接口
 */
class MockController extends Controller {
  /**
   * @summary mock
   * @description 这是一个mock接口
   * @Router GET /mock-api
   */
  async index() {
    const { ctx } = this;
    ctx.body = {
      code: 200,
      reqUrl: ctx.request.url,
      reqMethod: ctx.request.method,
      reqData: ctx.request.body,
      data: {
        time: new Date(),
        navList: [],
        cateList: [],
        linkList: [],
      },
      msg: '获取成功',
    };
  }

  /**
   * @summary mock 获取列表数据
   * @description 这是一个mock接口 获取列表数据
   * @Router POST /mock-api/list
   */
  async list() {
    console.log('mock list -- >', new Date().getTime());
    const { ctx } = this;

    var { pageNo = 1, pageSize = 10, keyword } = ctx.request.body;
    let pageList = mockData.creatPageList(pageNo, pageSize, keyword);
    let resData = {
      code: 200,
      reqUrl: ctx.request.url,
      reqMethod: ctx.request.method,
      reqData: ctx.request.body,
      page: pageList.pageData,
      data: {
        page: pageList.pageData,
        list: pageList.list,
      },
      dataList: pageList.list,
    };
    ctx.body = resData;
  }

  /**
   * @summary mock 获取详情
   * @description 这是一个mock接口 获取详情
   * @Router GET /mock-api/detail
   */
  async detail() {
    const { ctx } = this;

    let resData = {
      code: 200,
      reqUrl: ctx.request.url,
      reqMethod: ctx.request.method,
      reqData: ctx.request.body,
      data: mockData.creatDetail(),
    };
    ctx.body = resData;
  }

  /**
   * @summary mock 保存
   * @description 这是一个mock接口 保存
   * @Router POST /mock-api/save
   */
  async save() {
    const { ctx } = this;
    ctx.body = {
      code: 200,
      reqUrl: ctx.request.url,
      reqMethod: ctx.request.method,
      reqData: ctx.request.body,
      data: mockData.creatDetail(),
      msg: '保存成功',
    };
  }

  /**
   * @summary mock 删除
   * @description 这是一个mock接口 删除
   * @Router DELETE /mock-api/delete
   */
  async delete() {
    const { ctx } = this;
    ctx.body = {
      code: 200,
      reqData: ctx.request.body,
      data: mockData.creatDetail(),
      msg: '删除成功',
    };
  }
}

module.exports = MockController;
